<?php

namespace apiend\modules\school\models;

use Yii;
use yii\data\ActiveDataProvider;
use common\helpers\Url;
use common\helpers\ArrayHelper;

/**
 * 学校快速检索模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class SchoolSearch extends \common\models\school\School {

    //搜索
    public function search($params = array()) {
        $name = ArrayHelper::getValue($params, 'name');
        $code = ArrayHelper::getValue($params, 'area');

        $query = static::find()
        ->where([
            'status' => static::STATUS_ACTIVE,
        ]);
        $query->andFilterWhere(['like', 'name', $name]);
        if ($code) {
            $query->andFilterWhere(['district' => $code]);
        }
        $query->orderBy(['grade' => SORT_DESC]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        Yii::$app->db->cache(function($db) use($dataProvider) {
            $dataProvider->prepare();
        }, 60);

        return $dataProvider;
    }

    public function fields() {
        return [
            'id',
            'name',
            'grade',
            'cover' => function () {
                return Url::toRemote($this->cover);
            },
            'region',
            'address_detail',
            'link' => function () {
                return Url::h5('/school/' . $this->id);
            },
        ];
    }

}
